package com.lzs.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Area;
import com.lzs.PO.Page;
import com.lzs.PO.RuralCmsOffice;
import com.lzs.Service.AreaService;
import com.lzs.Service.RuralCmsOfficeService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/ruralcmsoffice")
public class RuralCmsOfficeController {
	@Autowired
	RuralCmsOfficeService ruralcmsofficeService;
	@Autowired
	AreaService areaService;

	@RequestMapping(value = "/ruralcmsoffice_list")
	public String ruralcmsofficeList(Model model, HttpServletRequest request,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,
			String keyword) throws Exception {

		Page<RuralCmsOffice> page = this.ruralcmsofficeService.findRuralCmsOfficePageList(keyword, pageSize, pageNum);

		model.addAttribute("page", page);

		return "/ruralcmsoffice/ruralcmsoffice_list";
	}

	@RequestMapping(value = "/ruralcmsoffice_add")
	public String addRuralCmsOffice() {

		return "/ruralcmsoffice/ruralcmsoffice_add";
	}

	@ResponseBody
	@RequestMapping(value = "/ruralcmsoffice_check")
	public Msg checkRuralCmsOffice(String office_name) {

		Boolean b = this.ruralcmsofficeService.countRuralCmsOfficeByName(office_name);

		if (b) {

			return Msg.success();

		} else {

			return Msg.fail();

		}
	}

	@RequestMapping(value = "/ruralcmsoffice_update")
	public String updateRuralCmsOffice(String office_id, Model model) {

		RuralCmsOffice ruralcmsoffice = this.ruralcmsofficeService.findRuralCmsOfficeById(office_id);

		Area area = this.areaService.findAreaById(ruralcmsoffice.getArea_id());

		model.addAttribute("ruralcmsoffice", ruralcmsoffice);

		model.addAttribute("area", area);

		return "/ruralcmsoffice/ruralcmsoffice_update";

	}

	@ResponseBody
  @RequestMapping(value = "/ruralcmsoffice_update_action")
  public Msg updateRuralCmsOfficeAction(String office_id,String office_name,String area_id,Integer office_rank,Integer area_rank) {
		 
	  Map<String,Object>map = new HashMap<String,Object>();
	   
	   map.put("office_id", office_id);
	   
	   map.put("office_name", office_name);
	   
	   map.put("area_id", area_id);
	   
	   map.put("area_rank",area_rank);
	   
	   map.put("office_rank",office_rank);
	   
	   if(area_rank<2 && office_rank<2 ) {
		   
		   this.ruralcmsofficeService.updateRuralCmsOffice(map);
		   
		   return Msg.success();
		   
	   }else if(area_rank==2 && office_rank>=2){
		   
		   this.ruralcmsofficeService.updateRuralCmsOffice(map);
		   
		   return Msg.success();
		   
	   }else {
		   
		   return Msg.fail();}
		   
	   }
	@ResponseBody
	@RequestMapping(value = "/ruralcmsoffice_delete")
	public Msg deleteRuralCmsOffice(String office_id) {
		
		try {
			this.ruralcmsofficeService.deleteRuralCmsOffice(office_id);
			
			return Msg.success();
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	}

	@ResponseBody
	@RequestMapping(value = "/ruralcmsoffice_add_action")
	public Msg addRuralCmsOfficeAction(String office_id, String office_name, String area_id, Integer office_rank,
			Integer area_rank) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("office_id", office_id);

		map.put("office_name", office_name);

		map.put("area_id", area_id);

		map.put("area_rank", area_rank);

		map.put("office_rank", office_rank);

		if (area_rank < 2 && office_rank < 2) {

			this.ruralcmsofficeService.addRuralCmsOffice(map);

			return Msg.success();

		} else if (area_rank == 2 && office_rank >= 2) {

			this.ruralcmsofficeService.addRuralCmsOffice(map);

			return Msg.success();

		} else {

			return Msg.fail();

		}
	}

}
